package com.linkoog.devtools.code_generator.setting.configurable.ui.template;

import com.linkoog.devtools.code_generator.template.model.node.FileNodeData;
import com.linkoog.devtools.code_generator.util.FileTypeUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.ui.Messages;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class TemplateFilePanelBuilder implements PanelBuilder<FileNodeData> {

    private JPanel myMainPanel;
    private JTextField nameTextField;
    private JPanel infoPanel;
    private JLabel describeLabel;
    private JPanel editerPanel;
    private JScrollPane describeScrollPanel;
    private JButton saveButton;


    private final EditorFactory factory = EditorFactory.getInstance();
    private final EditorHighlighterFactory highlighterFactory = EditorHighlighterFactory.getInstance();

    private Editor editor;

    // 显示时，当前fileNodeData对象
    private FileNodeData currentFileNodeData;


    public TemplateFilePanelBuilder() {

        nameTextField.setEditable(false);
        this.infoPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
        this.editor = createEditor("", "vm");
        this.myMainPanel.setPreferredSize(JBUI.size(340, 500));

        this.describeLabel.setText(text());
        this.editerPanel.add(this.editor.getComponent(), BorderLayout.CENTER);

        this.saveButton.addActionListener(e -> {
            if (currentFileNodeData != null) {
                currentFileNodeData.setTemplate(editor.getDocument().getText());
            }
        });

    }


    private String text() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<html>");
        stringBuilder.append("模板使用大").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("丰东股份霍比特人核桃仁").append("<br/>");
        stringBuilder.append("</html>");
        return stringBuilder.toString();
    }


    /**
     * 创建编辑器
     */
    private Editor createEditor(String templateContext, String extension) {
        Document velocityTemplate = factory.createDocument(templateContext);
        FileType fileType = FileTypeUtil.getFileType(extension);
        Editor editor = factory.createEditor(velocityTemplate, null, fileType, false);

        EditorSettings editorSettings = editor.getSettings();
        editorSettings.setLineNumbersShown(true);
        editorSettings.setAdditionalPageAtBottom(true);

        EditorHighlighter highlighter = highlighterFactory.createEditorHighlighter(null, fileType);
        ((EditorEx) editor).setHighlighter(highlighter);
        return editor;
    }

    public JPanel getMyMainPanel() {
        return myMainPanel;
    }


    private boolean isModified() {
        if (currentFileNodeData == null) {
            return false;
        }

        if (nameTextField.getText().equals(currentFileNodeData.getName()) && editor.getDocument().getText().equals(currentFileNodeData.getTemplate())) {
            return false;
        } else {
            return true;
        }
    }


    @Override
    public void disappear() {
        if (currentFileNodeData != null && isModified()) {
            int result = Messages.showYesNoDialog("是否保存修改？", "保存提示", Messages.getQuestionIcon());
            if (result == Messages.YES) {
                currentFileNodeData.setName(nameTextField.getText());
                currentFileNodeData.setTemplate(editor.getDocument().getText());
            }
        }
        nameTextField.setText("");
        ApplicationManager.getApplication().runWriteAction(() -> {
            editor.getDocument().setText("");
        });
    }

    @Override
    public void resetUI(FileNodeData data) {
        this.currentFileNodeData = data;
        resetData();
    }

    public void resetData() {
        if (currentFileNodeData != null) {
            nameTextField.setText(currentFileNodeData.getName());
            ApplicationManager.getApplication().runWriteAction(() -> {
                Document doc = editor.getDocument();
                doc.setText(currentFileNodeData.getTemplate() == null ? "" : currentFileNodeData.getTemplate());
            });
        }
    }


    private void createUIComponents() {
        // TODO: place custom component creation code here
        describeLabel = new ScrollableLabel();
        describeLabel.setBorder(BorderFactory.createEmptyBorder());
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new BorderLayout(0, 0));
        infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        myMainPanel.add(infoPanel, BorderLayout.NORTH);
        final JLabel label1 = new JLabel();
        label1.setText("名称");
        infoPanel.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        nameTextField = new JTextField();
        infoPanel.add(nameTextField, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
        myMainPanel.add(panel1, BorderLayout.CENTER);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, new Dimension(-1, 120), new Dimension(-1, 120), new Dimension(-1, 120), 0, false));
        describeScrollPanel = new JScrollPane();
        panel2.add(describeScrollPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        describeScrollPanel.setBorder(BorderFactory.createTitledBorder(null, "说明", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        describeLabel = new JLabel();
        describeLabel.setText("Label");
        describeScrollPanel.setViewportView(describeLabel);
        final Spacer spacer1 = new Spacer();
        panel1.add(spacer1, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        editerPanel = new JPanel();
        editerPanel.setLayout(new BorderLayout(0, 0));
        panel1.add(editerPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel3, new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        saveButton = new JButton();
        saveButton.setText("保存");
        panel3.add(saveButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer2 = new Spacer();
        panel3.add(spacer2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }

}
